/**
 * My simple header template from child directory
 * ConflictCell.java - ${projectName} - ${organizationName} - ${inceptionYear}
 */
package FourthTry.concurrent;

/*
 * #%L
 * Ability-tester
 * %%
 * Copyright (C) 2014 AmazingSERKU
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import FourthTry.Cell;
import FourthTry.Coordinates;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 22.01.14
 * Time: 19:33
 * To change this template use File | Settings | File Templates.
 */
public class ConflictCell {

    private Coordinates coordinates;
    private ConcurrentTreasureHunter mainCandidate;
    private List<ConcurrentTreasureHunter> candidates;

    public ConflictCell(ConcurrentTreasureHunter hunter, Coordinates coordinates) {
        this.coordinates = coordinates;
        this.mainCandidate = hunter;
        this.candidates = new ArrayList<ConcurrentTreasureHunter>();
    }

    public void addHunter(ConcurrentTreasureHunter hunter) {
        if (mainCandidate == null) {
            mainCandidate = hunter;
        } else {
            chooseHunter(hunter);
        }
    }

    private void chooseHunter(ConcurrentTreasureHunter candidate) {
        if (mainCandidate.getPower() > candidate.getPower()) {
            candidates.add(candidate);
            return;
        }
        if (mainCandidate.getPower() < candidate.getPower()) {
            candidates.add(mainCandidate);
            mainCandidate = candidate;
            return;
        }
        if (Math.random()<0.5) {
            candidates.add(mainCandidate);
            mainCandidate = candidate;
        } else {
            candidates.add(candidate);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ConflictCell that = (ConflictCell) o;
        return coordinates.equals(that.coordinates);
    }

    @Override
    public int hashCode() {
        return coordinates != null ? coordinates.hashCode() : 0;
    }

    public void resolveTargets() {
        if (candidates != null) {
            for (ConcurrentTreasureHunter hunter: candidates) {
                hunter.setCell(waitingCell(hunter.getCoordinates()));
            }
        }
    }

    public Coordinates getCoordinates() {
        return coordinates;
    }

    private Cell waitingCell(Coordinates coordinates) {
        return new Cell(coordinates);
    }
}
